﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Control;
using System.Windows.Forms;

namespace Krile.Forms.MainForm.Controls.InputMethodManager
{
    public class DefaultInput : UserControl,IInputMethod
    {
        private TextBox inputText;
        private Button DoPostButton;
        private Label description;

        public Krile.Kernel.Bridges.IInputControlManager ParentICM { get; set; }

        public DefaultInput()
            :base()
        {
            InitializeComponent();
            this.inputText.Font = Core.Config.Appearance.Font.Value;
        }

        public void Clear()
        {
            inputText.Clear();
        }

        public void FocusToInput()
        {
            inputText.Focus();
        }

        public string IdentifyString
        {
            get { return null; }
        }

        public string InputText
        {
            get
            {
                return inputText.Text;
            }
            set
            {
                inputText.Text = value;
            }
        }

        private void InitializeComponent()
        {
            this.inputText = new System.Windows.Forms.TextBox();
            this.DoPostButton = new System.Windows.Forms.Button();
            this.description = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // inputText
            // 
            this.inputText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.inputText.Location = new System.Drawing.Point(3, 3);
            this.inputText.Margin = new System.Windows.Forms.Padding(3, 3, 3, 0);
            this.inputText.Multiline = true;
            this.inputText.Name = "inputText";
            this.inputText.Size = new System.Drawing.Size(588, 70);
            this.inputText.TabIndex = 0;
            this.inputText.TextChanged += new System.EventHandler(this.MainText_TextChanged);
            this.inputText.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.DefaultInput_PreviewKeyDown);
            this.inputText.KeyDown += new System.Windows.Forms.KeyEventHandler(this.DefaultInput_KeyDown);
            // 
            // DoPostButton
            // 
            this.DoPostButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.DoPostButton.Enabled = false;
            this.DoPostButton.Location = new System.Drawing.Point(476, 76);
            this.DoPostButton.Name = "DoPostButton";
            this.DoPostButton.Size = new System.Drawing.Size(115, 27);
            this.DoPostButton.TabIndex = 1;
            this.DoPostButton.Text = "POST [ 0 ]";
            this.DoPostButton.UseVisualStyleBackColor = true;
            this.DoPostButton.Click += new System.EventHandler(this.DoPostButton_Click);
            // 
            // description
            // 
            this.description.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.description.AutoSize = true;
            this.description.ForeColor = System.Drawing.SystemColors.GrayText;
            this.description.Location = new System.Drawing.Point(3, 83);
            this.description.Margin = new System.Windows.Forms.Padding(3);
            this.description.Name = "description";
            this.description.Size = new System.Drawing.Size(185, 12);
            this.description.TabIndex = 2;
            this.description.Text = "Enter : Newline , Ctrl + Enter : Post";
            // 
            // DefaultInput
            // 
            this.Controls.Add(this.description);
            this.Controls.Add(this.DoPostButton);
            this.Controls.Add(this.inputText);
            this.Name = "DefaultInput";
            this.Size = new System.Drawing.Size(594, 106);
            this.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.DefaultInput_PreviewKeyDown);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.DefaultInput_KeyDown);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void DoPostButton_Click(object sender, EventArgs e)
        {
            PostCallback.Invoke();
        }

        private void DefaultInput_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.IsInputKey = true;
        }

        private void DefaultInput_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            e.Handled = true;
            if (e.Control && e.KeyCode == Keys.A)
                inputText.SelectAll();
            else if (e.KeyCode == Keys.Enter && e.Control)
                PostCallback.Invoke();
            else if (e.KeyData == Keys.Escape)
                CancelCallback.Invoke();
            else
            {
                e.SuppressKeyPress = false;
                e.Handled = false;
            }
        }

        public void Cancelled()
        {
            this.inputText.Clear();
        }

        private void MainText_TextChanged(object sender, EventArgs e)
        {
            DoPostButton.Text = "POST [" + inputText.Text.Length + "]";
            DoPostButton.Enabled = inputText.Text.Length > 0;
        }

        public bool OnReplyRequired(ulong t) { return true; }

        public MethodInvoker CancelCallback { get; set; }

        public object ExtraData { get { return null; } set { } }

        public MethodInvoker PostCallback { get; set; }
    }
}
